home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_7 / gauss.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  962 b   |  41 lines  |  [MATF/MATL]

  1. function [Q,N,X,quad,err] = gauss(f,a,b,A,W,toler)
  2. % [Q,N,X,quad,err] = gauss(f,a,b,A,W,toler)
  3. % Gaussian quadrature.
  4. % f is the name of the function, input.
  5. % a is the left  endpoint, input.
  6. % b is the right endpoint, input.
  7. % A is the table of abscissas, input.
  8. % W is the table of weights, input.
  9. % toler is the tolerance, input.
  10. % Q is the table of values, output.
  11. % N is the number of abscissas used, output.
  12. % X is the abscissas that were used, output.
  13. % quad is the quadrature value, output.
  14. % err is the error estimate, output.
  15. mid  = (a+b)/2;
  16. wide = (b-a)/2;
  17. err  = 1;
  18. j = 1;
  19. x = mid + A(1,1);
  20. N(1) = 1;
  21. Q(1)= W(1,1)*feval(f,x)*wide;
  22. while (((err>toler)|(j<4))&(j<17))
  23.   j = j+1;
  24.   sum1 = 0;
  25.   i = j;
  26.   if (j>10),
  27.     i = 12 + 4*(j-11);
  28.   end
  29.   if (j>14),
  30.     i = 24 + 8*(j-14);
  31.   end
  32.   for k = 1:i,
  33.     X(k) = mid + A(j,k)*wide;
  34.     sum1 = sum1 + W(j,k)*feval(f,X(k));
  35.   end
  36.   N(j) = i;
  37.   Q(j) = sum1*wide;
  38.   quad = Q(j);
  39.   err = abs(Q(j)-Q(j-1));
  40. end
  41.